package com.czmh.etc.onlinepubsys.controller.cmg;

import com.czmh.etc.onlinepubsys.constants.cmg.BusinessType;
import com.czmh.etc.onlinepubsys.entity.mingchuang.ApplyInfo;
import com.czmh.etc.onlinepubsys.entity.mingchuang.CurrentContext;
import com.czmh.etc.onlinepubsys.enums.cmg.ApiCodeEnum;
import com.czmh.etc.onlinepubsys.json.cmg.JSONResult;
import com.czmh.etc.onlinepubsys.request.cmg.ApplyOnlineRequest;
import com.czmh.etc.onlinepubsys.request.cmg.ConfirmOnlineRequest;
import com.czmh.etc.onlinepubsys.response.cmg.ApiResponseBase;
import com.czmh.etc.onlinepubsys.services.mingchuang.ICurrentContextService;
import com.czmh.etc.onlinepubsys.services.mingchuang.IEtcApplyService;
import com.czmh.etc.onlinepubsys.utils.cmg.CmgHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/{version}/api/cmg/sign")
@Api(tags = "线上银行签约接口相关 - 招商局")
public class CmgSignController {
    @Value("${cmg.accessno}")
    private String accessno;

    @Value("${cmg.issueOrgCode}")
    private String issueOrgCode;

    @Autowired
    private ICurrentContextService currentContextService;

    @Autowired
    private IEtcApplyService etcApplyService;


    @PostMapping("/applyOnline")
    @ApiOperation("线上银行签约申请")
    public JSONResult applyOnline(@RequestBody ApplyOnlineRequest req) {
        JSONResult result = new JSONResult();
        log.info("applyOnline:{}", req);
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        req.setIssueOrgCode(issueOrgCode);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.SIGN_APPLY_ONLINE, req);
        if(r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())){
            if(r.bizContent != null){
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
                CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
                ApplyInfo applyInfo = etcApplyService.get(ctx.getApplyId());

                applyInfo.setBankCardNo(req.getBankCardNo());
                applyInfo.setBankSn(req.getChannelType());
                applyInfo.setBankCardType(req.getBankCardType());
                etcApplyService.update(applyInfo);
            }
        }else{
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/confirmOnline")
    @ApiOperation("线上银行签约确认")
    public JSONResult confirmOnline(@RequestBody ConfirmOnlineRequest req) {
        JSONResult result = new JSONResult();
        log.info("confirmOnline:{}", req);
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        req.setIssueOrgCode(issueOrgCode);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.SIGN_CONFIRM_ONLINE, req);
        if(r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())){
            if(r.bizContent != null){
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());

                //String signChannelId = JSON.parseObject(result.getRspData()).getString("signChannelId");
                CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
                //log.info("短信签约确认签约成功,保存资金账户ID:{}", signChannelId);

                //更新进行时
                //ctx.setSignChannelId(signChannelId);
                ctx.setTransNo(req.getTradeNo());
                currentContextService.update(ctx);

                ApplyInfo applyInfo = etcApplyService.get(ctx.getApplyId());
                applyInfo.setBankSignTime(new Date());
                applyInfo.setState(1);
                etcApplyService.update(applyInfo);
            }
        }else{
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

}
